package com.course.app.transformer;

import com.course.app.api.data.TaskLearnDTO;
import com.course.domain.model.TaskGroup;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by Justin on 2017/5/30.
 */
public class TaskLearnTransformer {

    public static Map<Integer, TaskLearnDTO> toLearnMap(List<TaskGroup> taskGroupList) {
        return taskGroupList.stream()
                .collect(Collectors.groupingBy(TaskGroup::getTaskId))
                .entrySet().stream()
                .map(entry -> toLearn(entry.getKey(), entry.getValue()))
                .collect(Collectors.toMap(TaskLearnDTO::getId, Function.identity()));
    }

    private static TaskLearnDTO toLearn(int taskId, List<TaskGroup> taskGroupList) {
        TaskLearnDTO learnDTO = new TaskLearnDTO();
        learnDTO.setId(taskId);
        learnDTO.setItems(taskGroupList.stream().map(TaskLearnTransformer::toLearnItem).collect(Collectors.toList()));
        return learnDTO;
    }

    private static TaskLearnDTO.Item toLearnItem(TaskGroup taskGroup) {
        TaskLearnDTO.Item item = new TaskLearnDTO.Item();
        item.setIndex(taskGroup.getIdx());
        item.setItemId(taskGroup.getItemId());
        item.setType(toLearnType(taskGroup.getType()));
        item.setGroupId(taskGroup.getId());
        return item;
    }

    private static TaskLearnDTO.Type toLearnType(TaskGroup.Type type) {
        switch (type) {
            case AUDIO:
                return TaskLearnDTO.Type.AUDIO;
            case VIDEO:
                return TaskLearnDTO.Type.VIDEO;
            case STORY:
                return TaskLearnDTO.Type.STORY;
            default:
                return null;
        }
    }
}
